package com.taotao.service.impl;

import com.taotao.entity.Item;
import com.taotao.entity.ItemDesc;
import com.taotao.entity.ItemParam;
import com.taotao.entity.ItemParamItem;
import com.taotao.mapper.ItemDescMapper;
import com.taotao.mapper.ItemMapper;
import com.taotao.mapper.ItemParamItemMapper;
import com.taotao.mapper.ItemParamMapper;
import com.taotao.service.ItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * author: 王明跃
 * Create at: 18/7/6下午2:41
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Resource
    private ItemParamMapper itemParamMapper;
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private ItemParamItemMapper itemParamItemMapper;
    @Resource
    private ItemDescMapper itemDescMapper;

    @Override
    @Transactional
    public void addItemParam(long id, String paramData) {
        ItemParam itemParam = new ItemParam();
        itemParam.setItemCatId(id);
        itemParam.setParamData(paramData);
        Date date = new Date();
        itemParam.setCreated(date);
        itemParam.setUpdated(date);
        itemParamMapper.insertSelective(itemParam);
    }

    @Override
    public List<Item> findAll() {
        return itemMapper.findAll();
    }

    @Override
    public List<ItemParam> findItemParamAll() {
        return itemParamMapper.findItemParamAll();
    }

    @Override
    public int updateItemParam(Long itemCatId, String paramData) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updated = simpleDateFormat.format(date);
        return itemParamMapper.updateItemParam(itemCatId, paramData, updated);
    }

    @Override
    public int delItemParam(Long[] ids) {
        return itemParamMapper.delItemParam(ids);
    }


    @Override
    public boolean addItemParamItem(ItemParamItem itemParamItem) {
        itemParamItemMapper.insert(itemParamItem);
        if (itemParamItem.getId() != null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean addItemDesc(ItemDesc itemDesc) {
        itemDescMapper.insert(itemDesc);
        if (itemDesc.getItemId() != null) {
            return true;
        }
        return false;
    }

    @Override
    public Long addItem(Item item) {
        itemMapper.insert(item);
        return item.getId();
    }

    @Override
    public ItemParam findCidByItemParam(Long cid) {
        return itemParamMapper.findItemParamByCid(cid);
    }
}
